package com.mt.roll.wms.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mt.roll.core.api.Assert;
import com.mt.roll.core.base.BaseService;
import com.mt.roll.core.dal.entity.Area;
import com.mt.roll.core.dal.entity.Shelf;
import com.mt.roll.core.dal.service.ShelfServiceBiz;
import com.mt.roll.core.params.IdParam;
import com.mt.roll.core.vo.IdVo;
import com.mt.roll.core.vo.PageVo;
import com.mt.roll.core.vo.R;
import com.mt.roll.wms.mapper.AreaShelfVoMapper;
import com.mt.roll.wms.params.AreaLocationQueryParam;
import com.mt.roll.wms.params.AreaParam;
import com.mt.roll.wms.params.AreaShelfParam;
import com.mt.roll.wms.params.AreaShelfQueryParam;
import com.mt.roll.wms.service.AreaShelfService;
import com.mt.roll.wms.vo.AreaLocationVo;
import com.mt.roll.wms.vo.AreaShelfVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @auther yanyang
 * @date 2021/11/29 10:56
 */

@Service
@Transactional
public class AreaShelfServiceImpl extends BaseService implements AreaShelfService {

    @Autowired
    private ShelfServiceBiz shelfServiceBiz;

    @Autowired
    private AreaShelfVoMapper areaShelfVoMapper;

    @Override
    public R<PageVo<AreaShelfVo>> shelf(AreaShelfQueryParam areaShelfQueryParam) {
        IPage<AreaShelfVo> page = areaShelfVoMapper.shelf(new Page<>(areaShelfQueryParam.getCurrent(),areaShelfQueryParam.getSize()), areaShelfQueryParam);
        return successful(new PageVo<>(page, AreaShelfVo.class));
    }

    @Override
    public R<AreaShelfVo> shelfById(IdParam idParam) {
        Shelf shelf = shelfServiceBiz.getById(idParam.getId());
        Assert.notNull("详情不存在", shelf);
        AreaShelfVo areaShelfVo = areaShelfVoMapper.shelfById(idParam.getId());
        return successful(areaShelfVo);
    }

    @Override
    public R<IdVo> addShelf(AreaShelfParam areaShelfParam) {
        QueryWrapper<Shelf> wrapper = new QueryWrapper<>();
        wrapper.eq(Shelf.CODE, areaShelfParam.getCode());
        Assert.eqZero(shelfServiceBiz.count(wrapper), "该货架已经存在，请勿重复添加");
        Shelf shelf = new Shelf();
        BeanUtils.copyProperties(areaShelfParam, shelf);
        setCommonField(shelf);
        shelfServiceBiz.save(shelf);
        return successful(IdVo.builder().id(shelf.getId()).build());
    }

    @Override
    public R<IdVo> updateShelf(AreaShelfParam areaShelfParam) {
        Shelf shelf = shelfServiceBiz.getById(areaShelfParam.getId());
        //修改信息需要检验是否已存在
        if (!shelf.getCode().equals(areaShelfParam.getCode())) {
            QueryWrapper<Shelf> wrapper = new QueryWrapper<>();
            wrapper.eq(shelf.CODE, areaShelfParam.getCode());
            Assert.eqZero(shelfServiceBiz.count(wrapper),"该货架已经存在");
        }
        Shelf updateShelf = new Shelf();
        BeanUtils.copyProperties(areaShelfParam, updateShelf);
        setUpdateCommonField(updateShelf);
        shelfServiceBiz.updateById(updateShelf);
        return successful(IdVo.builder().id(shelf.getId()).build());
    }

    @Override
    public R<IdVo> deleteShelf(IdParam idParam) {
        Shelf shelf = shelfServiceBiz.getById(idParam.getId());
        if (shelf == null) {
            return failed("货架不存在");
        }
        shelfServiceBiz.removeById(idParam.getId());
        return successful(IdVo.builder().id(shelf.getId()).build());
    }
}
